import { baseRequestClient, requestClient } from '#/api/request';
import { PASSWORD } from "#/utils/constants"

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    userName?: string;
    phone?: string;
    verificationCode?: string;
    type?: string;
    code?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    token: string;
    refreshToken: string;
  }

  export interface RefreshTokenResult {
    data: LoginResult;
    status: number;
  }
}

export const refreshApi = "/auth/refresh-token"


/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  let url = '/auth/login/phone-password'
  if (data.type === PASSWORD) {
    data.hasOwnProperty('verificationCode') && (delete data.verificationCode)
  } else {
    data.hasOwnProperty('password') && (delete data.password)
    url = '/auth/login/phone-code'
  }
  data.hasOwnProperty('type') && (delete data.type)
  return requestClient.post<AuthApi.LoginResult>(url, data);
}

/**注册 */
export async function registerApi(data: any) {
  return requestClient.post('/auth/register', data);
}

/**重置密码*/
export async function resetPasswordApi(data: any) {
  return requestClient.patch('/auth/forgotten-password', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return requestClient.post<AuthApi.LoginResult>(refreshApi);
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return requestClient.post('/auth/login-out');
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}

/**
 * 获取手机验证码
 */
export async function getVerificationCodeApi(params: { phoneNumber: string }) {
  return requestClient.get('/system/get-verification-code', { params });
}

export async function getVerificationOpenCodeApi(params: { phoneNumber: string, captchaKey: string }) {
  return requestClient.get('/system/open/get-verification-code', { params });
}


/**获取邮箱验证码 */
export async function getEmailCodeApi(params: { email: string }) {
  return requestClient.get('/system/email/send-code', { params });
}

/**
 * 获取语音验证码
 */
export async function getVoiceVerificationCodeApi(params: { phoneNumber: string }) {
  return requestClient.get('/system/send-voice-call', { params });
}

export async function getVoiceVerificationOpenCodeApi(params: { phoneNumber: string, captchaKey: string }) {
  return requestClient.get('/system/open/send-voice-call', { params });
}

/** 校验手机验证码 */
export async function verifyPhoneCodeApi(data: { phoneNumber: string; verificationCode: string }) {
  return requestClient.post('/auth/verify/phone-code', data);
}

